package com.qying.rpc.provider.config;

import com.qying.rpc.common.RpcProperties;
import com.qying.rpc.provider.RpcProvider;
import com.qying.rpc.registry.RegistrType;
import com.qying.rpc.registry.RegistryFactory;
import com.qying.rpc.registry.RegistryService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties(RpcProperties.class)
public class RpcProviderAutoConfiguration {

    @Resource
    private RpcProperties properties;

    @Bean
    public RpcProvider init() throws Exception{
        RegistrType registrType = RegistrType.valueOf(properties.getRegistryType());

        RegistryService registryService = RegistryFactory.getInstance(properties.getRegistryAddr(), registrType);

        return new RpcProvider(properties.getServicePort(), registryService);
    }

}

